package com.sunmi.usercenter.ui.activity;

import com.sunmi.usercenter.bean.Info;
import com.sunmi.usercenter.bean.Task;
import com.sunmi.usercenter.controller.SuperController;
import com.sunmi.usercenter.ui.view.SuperUI;
import com.sunmi.usercenter.utils.ThreadPoolManager;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.Toast;

/**
 * 所有父的FragmentActivity
 * 
 * @author zhou.ni 2015年4月21日
 */
public abstract class SuperFragmentActivity extends FragmentActivity implements SuperUI, OnClickListener {

	protected boolean isAvtive = false;

	protected Context mContext;
	protected ProgressDialog progress;
	protected ThreadPoolManager mThreadPoolManager;
	/** 碎片管理器 **/
	protected FragmentManager fmm;
	protected SuperController mController;
	
	@SuppressWarnings("rawtypes")
	protected Info mInfo;
	protected final String INFO_NAME = "info";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mContext = this;
		isAvtive = true;
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		
		fmm = getSupportFragmentManager();
		mThreadPoolManager = ThreadPoolManager.getInstance();
		progress = getProgressDialog("正在加载,请稍后...");
		progress.setCancelable(true);
	}

	/**
	 * 当前最后执行的线程任务,task的ID属性可以用于判断线程启动的先后
	 */
	protected Task lastTask = new Task(0) {

		@Override
		public void run() {
			// TODO Auto-generated method stub
		}
	};

	/**
	 * 得到进度条
	 * 
	 * @param msg
	 * @return
	 */
	public ProgressDialog getProgressDialog(String msg) {
		ProgressDialog progressDialog = new ProgressDialog(this);
		// progressDialog.setIndeterminate(true);
		progressDialog.setMessage(msg);
		progressDialog.setCancelable(true);
		return progressDialog;
	}

	/**
	 * 弹出土司
	 * 
	 * @param msg
	 */
	public void showToastOnUI(final String msg) {
		runOnUiThread(new Runnable() {

			@Override
			public void run() {
				Toast.makeText(SuperFragmentActivity.this, msg, Toast.LENGTH_SHORT).show();
			}
		});

	}
	
	public void showToast(String msg) {
		Toast.makeText(SuperFragmentActivity.this, msg, Toast.LENGTH_SHORT).show();
	}

	@Override
	protected void onPause() {
		super.onPause();
		isAvtive = false;
	}

	@Override
	protected void onResume() {
		super.onResume();
		isAvtive = true;
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		isAvtive = false;
	}

	public boolean getAvtive() {
		return isAvtive;
	}

	protected void hintNoNetWork() {
		// TODO 弹出网络错误提示框
	}

	protected float getDimen(int id) {
		float dimension = getResources().getDimension(id);
		return dimension;
	}
	
	/**
	 * 获得配置文件参数
	 * 
	 * @param id
	 * @return
	 */
	public int getDimensPixelSize(int id) {
		return getResources().getDimensionPixelSize(id);
	}

	public void executeTask(Runnable run) {
		mThreadPoolManager.executeTask(run);
	}

	/**
	 * 加载图片
	 * 
	 * @param tuContainer
	 * @param item
	 */
	public void loadIMG(ImageView img, String url) {
//		Picasso.with(this).load(url).error(R.drawable.error).into(img);
	}
	public void loadIMG(ImageView img, String url, int errorId) {
//		Picasso.with(this).load(url).error(errorId).into(img);
	}

	public void loadIMG(ImageView img, int id) {
//		Picasso.with(this).load(id).error(R.drawable.error).into(img);
	}
	
	@Override
	public void loadIMG(ImageView img, String url, int placeholder, int errorId) {
//		Picasso.with(this).load(url).placeholder(placeholder).error(R.drawable.error).into(img);
		
	}

	/**
	 * 打开一个Activity
	 * 
	 * @param clazz
	 * @param info
	 */
	@SuppressWarnings("rawtypes")
	public void openActivity(final Class clazz, final Info info) {
		runOnUI(new Runnable() {

			@Override
			public void run() {
				Intent intent = new Intent();
				intent.setClass(mContext, clazz);
				if (info != null) {
					intent.putExtra(INFO_NAME, info);
				}
				startActivity(intent);
			}
		});

	}

	/**
	 * 打开一个Activity for result
	 * 
	 * @param clazz
	 * @param info
	 */
	@SuppressWarnings("rawtypes")
	public void openActivity4Result(final Class clazz, final Info info,
			final int requestCode) {
		runOnUI(new Runnable() {

			@Override
			public void run() {
				Intent intent = new Intent();
				intent.setClass(mContext, clazz);
				if (info != null) {
					intent.putExtra(INFO_NAME, info);
				}
				startActivityForResult(intent, requestCode);
			}
		});

	}

	/**
	 * 返回数据到上一个activity
	 * 
	 * @param resultCode
	 * @param data
	 */
	@SuppressWarnings("rawtypes")
	public void closeActivity4Result(int resultCode, Info data) {
		Intent intent = getIntent();
		intent.putExtra(INFO_NAME, data);
		setResult(resultCode, intent);
		finish();
	}


	/**
	 * UI线程执行一个任务
	 * 
	 * @param run
	 */
	public void runOnUI(Runnable run) {
		runOnUiThread(run);
	}

	/**
	 * 是否有数据传递过来
	 * 
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public boolean hasInfo() {
		mInfo = (Info) getIntent().getSerializableExtra(INFO_NAME);
		if (mInfo != null) {
			return true;
		}
		return false;
	}

	/**
	 * 获得配置文件参数
	 * 
	 * @param id
	 * @return
	 */
	public float getDimens(int id) {
		return getResources().getDimension(id);
	}
	
	@Override
	public Context getContext() {
		return this;
	}
	
	@Override
	public OnClickListener getClickListener() {
		return this;
	} 
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public void executeTask(Task task) {
		mThreadPoolManager.executeTask(task);
		
	}

	@Override
	public void showProgressDialog() {
		runOnUI(new Runnable() {

			@Override
			public void run() {
				if (progress == null) {
					progress = new ProgressDialog(mContext);
					progress.setMessage("正在加载,请稍后...");
					progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
				}
				progress.setCancelable(true);
				try {
					progress.show();
				} catch (Exception e) {
					e.printStackTrace();
				}

			}
		});
	}
	
	public void showProgressDialog(final String msg, final boolean isCancel) {
		runOnUI(new Runnable() {

			@Override
			public void run() {
				progress = new ProgressDialog(mContext);
				progress.setMessage(msg);
				progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
				progress.setCancelable(isCancel);
				try {
					progress.show();
				} catch (Exception e) {
					e.printStackTrace();
				}

			}
		});

	}

	@Override
	public void dismissProgressDialog() {
		runOnUI(new Runnable() {

			@Override
			public void run() {
				if (progress != null) {
					progress.dismiss();
				}

			}
		});
	}


}
